<script setup>
import { ref, onMounted, computed } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { ElMessage } from 'element-plus';
import { DocumentChecked, DocumentCopy, User, UserFilled, Menu } from '@element-plus/icons-vue';

const router = useRouter();
const route = useRoute();
const isCollapse = ref(false);
const user = ref(null);

// 侧边栏菜单项
const menuItems = [
  {
    index: 'orders',
    path: '/orders',
    label: '订单管理',
    icon: 'DocumentChecked'
  },
  {
    index: 'print-records',
    path: '/print-records',
    label: '打印记录',
    icon: 'DocumentCopy'
  },
  {
    index: 'users',
    path: '/users',
    label: '账号管理',
    icon: 'User'
  },
  {
    index: 'profile',
    path: '/profile',
    label: '个人中心',
    icon: 'UserFilled'
  }
];

// 获取当前激活的菜单项
const activeIndex = computed(() => {
  return route.path.split('/')[1] || 'orders';
});

// 初始化用户信息
onMounted(() => {
  const userStr = localStorage.getItem('user');
  if (userStr) {
    user.value = JSON.parse(userStr);
  }
});

// 处理菜单点击
const handleMenuClick = (index) => {
  const menuItem = menuItems.find(item => item.index === index);
  if (menuItem) {
    router.push(menuItem.path);
  }
};

// 切换侧边栏折叠状态
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value;
};

// 退出登录
const handleLogout = () => {
  localStorage.removeItem('user');
  ElMessage.success('已退出登录');
  router.push('/login');
};
</script>

<template>
  <div class="home-layout">
    <!-- 侧边栏 -->
    <el-aside
      :width="isCollapse ? '64px' : '240px'"
      class="sidebar"
      :collapse-transition="true"
    >
      <div class="sidebar-header">
        <h3 v-if="!isCollapse" class="logo-text">打印管理系统</h3>
        <el-icon v-if="isCollapse" class="logo-icon">
          <DocumentChecked />
        </el-icon>
      </div>
      
      <el-menu
        :default-active="activeIndex"
        class="sidebar-menu"
        @select="handleMenuClick"
        :collapse="isCollapse"
        :router="false"
        background-color="#001529"
        text-color="#fff"
        active-text-color="#409EFF"
      >
        <el-menu-item
          v-for="item in menuItems"
          :key="item.index"
          :index="item.index"
        >
          <component :is="item.icon" class="menu-icon"/>
          <template #title>{{ item.label }}</template>
        </el-menu-item>
      </el-menu>
    </el-aside>
    
    <!-- 主内容区域 -->
    <div class="main-container">
      <!-- 顶部导航 -->
      <el-header class="header">
        <div class="header-left">
          <el-button
            @click="toggleSidebar"
            circle
            plain
            :icon="Menu"
            class="sidebar-toggle"
          />
        </div>
        <div class="header-right">
          <el-dropdown @command="handleLogout">
            <span class="user-info">
              <el-avatar size="small" :src="user?.avatar || ''">
                {{ user?.username?.charAt(0) || 'U' }}
              </el-avatar>
              <span v-if="user" class="username">{{ user.username }}</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <!-- 内容区域 -->
      <el-main class="content">
        <router-view />
      </el-main>
    </div>
  </div>
</template>

<style scoped>
.el-menu-item{
  justify-content: center
}
.home-layout {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  background-color: #001529;
  transition: width 0.3s ease;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
}

.sidebar-header {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid #273346;
  padding: 0 20px;
}

.logo-text {
  color: #fff;
  font-size: 18px;
  margin: 0;
  font-weight: 600;
}

.logo-icon {
  color: #fff;
  font-size: 20px;
}

.sidebar-menu {
  height: calc(100vh - 60px);
  border-right: none;
}

/* 调整菜单图标大小 */
:deep(.menu-icon) {
  width:  26px;
}

:deep(.menu-icon svg) {
  width: 14px;
  height: 14px;
}

.main-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.header {
  height: 60px;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
}

.sidebar-toggle {
  color: #606266;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: #f0f2f5;
}

.username {
  margin-left: 8px;
  color: #303133;
}

.content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background-color: #f5f7fa;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    width: 64px !important;
  }
  
  .sidebar-header h3 {
    display: none;
  }
  
  .username {
    display: none;
  }
}
</style>